/**
 *
 * @author spider
 * Created on 01/05/2010
 */
package br.unb.utils;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

public class LogPrinter {

    private FileOutputStream fos;
    private PrintWriter pw;
    private LogFile logFile;

    public LogPrinter(LogFile log) {
        logFile = log;
        try {
            fos = new FileOutputStream(logFile, false);
            pw = new PrintWriter(fos, true);
        } catch (FileNotFoundException e) {
            System.err.println("Error opening file: " + logFile.toString());
            e.printStackTrace();
        }
    }

    public void write(String line) {
        pw.println(line);
    }

    public void write(byte[] data, int offSet, int len) throws IOException {
        if (data[0] != -1) {
            fos.write(data, offSet, len);
        } else {
            this.close();
        }

    }

    public void logEntry(
            double PacketNum,
            double PacketLength,
            double PacketSendTime,
            double PacketReceiveTime) {
        pw.println(
                PacketNum
                + "\t" + PacketLength
                + "\t" + PacketSendTime
                + "\t" + PacketReceiveTime);
    }

    public void close() {
        try {
            pw.close();
            fos.close();
        } catch (IOException e) {
            System.err.println("Error closing file: " + logFile.toString());
            e.printStackTrace();
        }
    }
}
